home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dnsconf / dnsconf.c < prev    next >
C/C++ Source or Header  |  1996-07-27  |  5KB  |  209 lines

  1. #pragma implementation
  2. #include "../misc/misc.h"
  3. #include "dnsconf.h"
  4. #include "internal.h"
  5. #include "../netconf/netconf.h"
  6. #include "dnsconf.m"
  7.  
  8. DNSCONF_HELP_FILE help_dnsconf ("intro");
  9.  
  10. static DNSCONF_HELP_FILE help_forward ("forwarders");
  11. extern CONFIG_FILE f_boot;
  12.  
  13. /*
  14.     Edit the forwarders IP number.
  15. */
  16. PUBLIC void DNS::editforwarders()
  17. {
  18.     DIALOG dia;
  19.     int i;
  20.     for (i=0; i<MAX_FORWARDERS; i++){
  21.         dia.newf_str (i==0 ? MSG_U(F_IPADR,"IP address") : "",forwarders[i]);
  22.     }
  23.     if (dia.edit (MSG_U(T_FORWARDERS,"Forwarders")
  24.         ,MSG_U(I_FORWARDERS,"Your DNS may delegate to other DNS\n"
  25.          "the resolution of external domain.\n"
  26.          "Just enter the IP number of each DNS\n"
  27.          "in priority order.")
  28.         ,help_forward.getpath()
  29.         ,0) == MENU_ACCEPT){
  30.         nbforward = 0;
  31.         for (i=0; i<MAX_FORWARDERS; i++){
  32.             if (!forwarders[i].is_empty()){
  33.                 nbforward++;
  34.             }
  35.         }
  36.         write();
  37.     }
  38. }
  39.  
  40.  
  41. PUBLIC void DNS::edit()
  42. {
  43.     /* #Specification: dnsconf / main menu
  44.     */
  45.     int choice=0;
  46.     while (1){
  47.         static const char *t_primarys = MSG_U(M_DOMAINS,"domains");
  48.         static const char *t_reverses = MSG_U(M_IPREV,"IP reverse mappings");
  49.         static const char *t_secondarys = MSG_U(M_SECOND,"secondarys");
  50.         static const char *t_forwarders = MSG_U(M_FORWARD,"forwarders");
  51.         static const char *t_ipmap = MSG_U(M_IPRANGE,"IP allocation space");
  52.         static const char *t_editrecs = MSG_U(M_HOSTINFO,"host information");
  53.         static const char *menuopt[]={
  54.             MSG_U(M_CONFIG,"Configure"),    t_primarys,
  55.             " ",        t_reverses,
  56.             " ",        t_secondarys,
  57.             " ",        t_forwarders,
  58.             " ",        t_ipmap,
  59.             "-",        "",
  60.             MSG_U(ADDEDIT,"Add/Edit"),    t_editrecs,
  61.             NULL
  62.         };
  63.         MENU_STATUS code = xconf_menu (
  64.             MSG_U(T_DNSCONF,"DNS configurator")
  65.             ,MSG_U(I_DNSCONF,"This package allows you to configure\n"
  66.              "a Domain Name Server (DNS)\n")
  67.             ,help_dnsconf
  68.             ,NULL
  69.             ,NULL
  70.             ,NULL
  71.             ,NULL
  72.             ,menuopt,choice);
  73.         if (code == MENU_QUIT || code == MENU_ESCAPE){
  74.             break;
  75.         }else{
  76.             const char *key = menuopt[choice*2+1];
  77.             if (key == t_primarys){
  78.                 primarys.edit(*this);
  79.             }else if (key == t_reverses){
  80.                 primarys_rev.edit(*this);
  81.             }else if (key == t_secondarys){
  82.                 secondarys.edit(*this);
  83.             }else if (key == t_forwarders){
  84.                 editforwarders();
  85.             }else if (key == t_ipmap){
  86.                 ipmap_edit();
  87.             }else if (key == t_editrecs){
  88.                 editrecs();
  89.             }
  90.         }
  91.     }
  92. }
  93.  
  94. /*
  95.     Find out if any part of the DNS was modified during the session
  96. */
  97. PUBLIC int DNS::was_modified()
  98. {
  99.     int ret = 0;
  100.     if (ARRAY_OBJ::was_modified()
  101.         || primarys.was_modified()
  102.         || secondarys.was_modified()){
  103.         ret = 1;
  104.     }
  105.     return ret;
  106. }
  107.         
  108. static DNS *cur_dns = NULL;
  109.  
  110. void dnsconf_edit()
  111. {
  112.     DNS dns;
  113.     if (!dns.empty()
  114.         || xconf_yesno(MSG_U(Q_NODNS,"No DNS configured")
  115.             ,MSG_U(I_NODNS,"Do you want to create one ?")
  116.             ,help_dnsconf)==MENU_YES){
  117.         dns.basiccheck();
  118.         
  119.         cur_dns = &dns;        // Hack for dnsconf_condset()
  120.                     // which is called by netconf_updatedns()
  121.         dns.edit();
  122.         cur_dns = NULL;
  123.     }
  124. }
  125.  
  126. /*
  127.     Update the DNS with a host definition.
  128.     If there is no DNS currently configured, it just do nothing.
  129.  
  130.     Return != 0 if the DNS was updated.
  131. */
  132. int dnsconf_condset(
  133.     const char *hostname,
  134.     const char *tbip[],
  135.     int nbip)
  136. {
  137.     int ret = 0;
  138.     if (cur_dns != NULL){
  139.         if (cur_dns->set (hostname,tbip,nbip) != -1){
  140.             ret = 1;
  141.         }
  142.     }else if (f_boot.exist()){
  143.         DNS dns;
  144.         if (dns.set (hostname,tbip,nbip) != -1){
  145.             dns.write();
  146.             ret = 1;
  147.         }
  148.     }
  149.     return ret;
  150. }
  151.  
  152. static void usage ()
  153. {
  154.     xconf_error (MSG_U(N_DNSUSAGE
  155.         ,"Dnsconf: Invalid arguments\n"
  156.          "dnsconf --set host ip_number\n"
  157.          "dnsconf --unset host\n"
  158.          "\n"
  159.         "dnsconf without argument start the interactive mode\n")
  160.         );
  161. }
  162.  
  163. int dnsconf_main (int argc, char *argv[])
  164. {
  165.     int ret = 0;
  166.     if (argc == 1){
  167.         dnsconf_edit();
  168.         netconf_checkupdate();
  169.     }else if (!f_boot.exist()){
  170.         xconf_error (MSG_U(E_NODNS,"No DNS currently configured"));
  171.     }else{
  172.         DNS dns;
  173.         if (dns.empty()){
  174.             xconf_error (MSG_R(E_NODNS));
  175.         }else if (argc >= 4
  176.             && strcmp(argv[1],"--set")==0){
  177.             /* #Specification: dnsconf / --set option
  178.                 The dnsconf options "--set" allows you to
  179.                 set or update the specification of a host.
  180.                 You must specify the name (fully qualified or
  181.                 not) followed by the IP number.
  182.  
  183.                 dnsconf --set newhost.mydomain 192.68.210.2
  184.             */
  185.             ret = dns.set (argv[2],(const char**)&argv[3],argc - 3);
  186.             if (ret != -1){
  187.                 dns.write();
  188.             }else{
  189.                 fprintf (stderr,MSG_U(E_NODOMAINS
  190.                     ,"Not applicable to any domain in the DNS\n"));
  191.             }
  192.         }else if (argc == 3
  193.             && strcmp(argv[1],"--unset")==0){
  194.             /* #Specification: dnsconf / --unset option
  195.                 The dnsconf options "--unset" allows you to
  196.                 remove a host specification from your DNS.
  197.  
  198.                 dnsconf --unset newhost.mydomain
  199.             */
  200.             dns.unset (argv[2]);
  201.             dns.write();
  202.         }else{
  203.             usage();
  204.         }
  205.     }
  206.     return ret;
  207. }
  208.  
  209.